<?php
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Support\Facades\Gate;

class PostCtrl extends Controller
{

    public function save(Request $req)
    {
        //判断用户是否有权限
        $post = Post::find($req->id);
        if ($post) {
            //修改
            if (Gate::allows('update', $post)) {
                return 'ok';
            } else {
                return '无权限';
            }

        } else {
            //新增
            if (!$req->user()->can('create', Post::class)) {
                return '你无法添加操作';
            }

            return '操作成功';
        }
        return 'error';
    }


}